//引入axios
import axios from "axios";

//默认地址
axios.defaults.baseURL = "/api/private/v1";

//请求拦截器
axios.interceptors.request.use(
  (config) => {
    //如果有token的话就把这个解开
    //   if (localStorage.token) {
    //     config.headers.Authorization = JSON.parse(localStorage.token);
    //   }

    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

//响应拦截器
axios.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

//封装get方法

export const get = (url, data = {}) => {
  return new Promise((resolve, reject) => {
    axios.get(url, { params: data }).then(
      (res) => {
        resolve(res);
      },
      (err) => {
        reject(err);
      }
    );
  });
};

//封装post方法
export const post = (url, data = {}) => {
  return new Promise((resolve, reject) => {
    axios.post(url, data).then(
      (res) => {
        resolve(res);
      },
      (err) => {
        reject(err);
      }
    );
  });
};

//封装delete方法
export const delete1 = (url, data = {}) => {
  return new Promise((resolve, reject) => {
    axios.delete(url, data).then(
      (res) => {
        resolve(res);
      },
      (err) => {
        reject(err);
      }
    );
  });
};

//封装put方法
export const put = (url, data = {}) => {
  return new Promise((resolve, reject) => {
    axios.put(url, data).then(
      (res) => {
        resolve(res);
      },
      (err) => {
        reject(err);
      }
    );
  });
};
